package com.yiren.manager;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {
	public static void main(String[] args) {
//		String sql = "select * from T1 where 1=1 and a = :aa  and (b =:bb or b =:cc) and e like :ee";
//		Map<String, Object> map = new HashMap<String, Object>();
//		map.put("aa","111");
//		map.put("bb", "222");
////		map.put("cc", "333");
////		map.put("dd", "444");
//		System.out.println(getRunnableSql(sql,map));
		
		System.out.println(0000012/3);
	}
	
	public static String getRunnableSql(String labelSql,Map<String, Object> param){
		 //匹配符合的字段
        String regx1 = "(or|and)?\\s?[\\w?]\\s?(=|like)?\\s?:\\w+";
        //匹配:后面的字符
        String regx2 = "(?<=:)\\w+"; 
 
        Pattern pattern1 = Pattern.compile(regx1);
 
        Pattern pattern2 = Pattern.compile(regx2);
 
        Matcher matcher = pattern1.matcher(labelSql); //匹配整条sql
        
        while (matcher.find()) {
            String paramVal = null;
            // 提取到要替换的一段条件
            String paramSql = matcher.group();
 
            Matcher matcher2 = pattern2.matcher(paramSql);
            // 找出:aa中的aa
            if (matcher2.find()) {
            	paramVal = matcher2.group();	
            }
            // 获取map中的参数值
            Object mapParam = param.get(paramVal);
            // 判断是否为null，替换成空
            if (mapParam == null) {
                labelSql = labelSql.replaceAll(paramSql, " ");
            } else {
                // 如果不为null,替换值
                labelSql = labelSql.replaceAll(":" + paramVal, mapParam.toString());
            }
            
            //匹配( or bb=333)类似的字段
            String regx3 = "\\(\\s+(and|or)\\s+[\\w?]\\s+(=|like)?\\s?\\w+\\s?\\)";
            Pattern pattern3 = Pattern.compile(regx3);//匹配括号里存在连接在前面的字段
            Matcher matcher3 = pattern3.matcher(labelSql);
            while (matcher3.find()) {
            	String regx4 = "\\s+(and|or)";
            	Pattern pattern4 = Pattern.compile(regx4);
            	Matcher matcher4 = pattern4.matcher(matcher3.group());
            	String condStr = matcher3.group();
            	System.out.println(condStr);
            	if(matcher4.find()){
            		String condParam = matcher4.group();//获得连接符，and|or
             		//将连接符替换为空，再替换掉括号里的内容
                 	labelSql = labelSql.replaceAll(condStr, condStr.replace(condParam, ""));
            	}
			}
            
            //匹配and(  )|or(  )这样的字段
            String regx5 = "\\s+(and|or)\\s+\\(\\s+\\)";
            Pattern pattern5 = Pattern.compile(regx5);
            Matcher matcher5 = pattern5.matcher(labelSql);
            while(matcher5.find()){
            	System.out.println(matcher5.group().trim());
            	labelSql = labelSql.replace(matcher5.group().trim(), "");
            	System.out.println(labelSql);
            }
        }
 
        return labelSql;
	}
	
}
